package stream;

import org.junit.BeforeClass;
import org.junit.Test;
import zsoft.core.util.ZReflectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;

/**
 *
 * Created by Robo on 2018/5/21.
 */
public class JunitTest {
    private static int MAX_SIZE = 10000000;
    private static List<Integer> list = new ArrayList<>();
    private static List<BigDecimal> bdList = new ArrayList<>();
    private static List<InnerNumber> objList = new ArrayList<>();

    static Random random = new Random(Integer.MAX_VALUE);
    static class InnerNumber{
        public InnerNumber(Integer integer) {
            this.integer = integer;
        }
        public InnerNumber(BigDecimal bigDecimal) {
            this.bigDecimal = bigDecimal;
        }

        private Integer integer;
        private BigDecimal bigDecimal;

        public Integer getInteger() {
            return integer;
        }

        public void setInteger(Integer integer) {
            this.integer = integer;
        }

        public BigDecimal getBigDecimal() {
            return bigDecimal;
        }

        public void setBigDecimal(BigDecimal bigDecimal) {
            this.bigDecimal = bigDecimal;
        }
    }


    @BeforeClass
    public static void initList(){
        System.out.println("init...");
        for (int i = 0; i < MAX_SIZE; i++) {
            int r = random.nextInt(Integer.MAX_VALUE);
//            InnerNumber in = new InnerNumber(r);
            BigDecimal br = new BigDecimal(r);
//            list.add(r);
//            objList.add(in);
            bdList.add(br);
        }
    }
    @Test
    public void forEachTest(){
        long ts = System.currentTimeMillis();
//        int max = Integer.MIN_VALUE;
        BigDecimal max = new BigDecimal(0);
//        for (Integer i : list) {
//            max = Integer.max(max, i);
//        for (InnerNumber i : objList) {
//            max = Integer.max(max, i.getInteger());
        for (BigDecimal i : bdList) {
            max = max.max(i);
        }
        System.out.println(System.currentTimeMillis()-ts+" foreach:"+max);
    }
    @Test
    public void lamadaTest(){
        long ts = System.currentTimeMillis();
//        int max = list.stream().reduce(Integer.MIN_VALUE, Math::max);
//        int max = objList.stream().map(InnerNumber::getInteger).reduce(Integer.MIN_VALUE, Math::max);
        BigDecimal max = bdList.stream().reduce(BigDecimal::max).get();
        System.out.println(System.currentTimeMillis()-ts+" lamada:" + max);
    }
    @Test
    public void testFirst(){
        List<Integer> list = new ArrayList();
        Optional<Integer> optional = list.stream().reduce((a, b) -> a + b);
        Integer integer = optional.orElse(0);

        System.out.println("1"+integer);
    }


    public void testReflect(){//要引用太123123多的包 暂时用不了
        Student student = new Student();
        ZReflectionUtils.invokeSetter(student, "name", "Stu");
        System.out.println(student);
        Object obj = (Object) student;
        String name = ZReflectionUtils.invokeGetter(obj, "name");
        ZReflectionUtils.invokeSetter(obj, "name", name+"_Obj");
        System.out.println((Student) obj);
    }
}
